package com.sgcc.sx.pmos.common.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.common.utils.ImmersionBarUtils;
import com.sgcc.sx.pmos.common.utils.SmartLoadUtil;
import com.sgcc.ln.pmos.common.widget.LoadingLayout;
import com.sgcc.ln.pmos.common.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChuPengDa
 * WhenCreated：2021/7/2 11:14
 * Description：
 */
public abstract class MvpBaseRecycleActivity<T, P extends BasePresenter> extends BaseActivity implements IView, LoadingLayout.RetryListener {

    protected P mPresenter;
    public List<T> mData = new ArrayList<>();
    public LinearLayout ll_content;
    public RecyclerView recycleview;
    public ImageButton floating_btn;
    public LoadingLayout loadinglayout;
    public SmartRefreshLayout refreshLayout;
    public RelativeLayout bottom_layout, header_layout, not_login_layout;
    public TitleBar title_bar;
    public View view_top_flag_bit;
    public BaseQuickAdapter adapter;
    public boolean isrefresh = false;//是否是刷新状态
    private boolean isshowContent = false;//是否一直显示content 不显示loadinglayout 失败页面

    public boolean isFloatingButtonVisibility = false;

    @Override
    protected void onCreate(Bundle arg) {
        mPresenter = createPresenter();
        //presenter取得与界面的联系
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        super.onCreate(arg);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_base_recyclelist;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void initViews() {
        super.initViews();
        receiveIntentExtras();
        title_bar = findViewById(R.id.title_bar);
        view_top_flag_bit = findViewById(R.id.view_top_flag_bit);
        title_bar.setVisibility(View.VISIBLE);
        ImmersionBarUtils.initColorBaseBar(this, getTitleBarColor() == 0 ? R.color.white : getTitleBarColor());
        title_bar.setBackground_color(getTitleBarColor() == 0 ? R.color.white : getTitleBarColor());
        title_bar.setTitle(getTitleText());
        setTitleBar(title_bar);
        header_layout = findViewById(R.id.header_layout);
        bottom_layout = findViewById(R.id.bottom_layout);
        recycleview = findViewById(R.id.recycleview);
        loadinglayout = findViewById(R.id.loadinglayout);
        refreshLayout = findViewById(R.id.refreshLayout);
        floating_btn = findViewById(R.id.floating_btn);
        ll_content = findViewById(R.id.ll_content);
        not_login_layout = findViewById(R.id.not_login_layout);

        loadinglayout.setEmptyBgColor(getEmptyBgColor() == 0 ? R.color.white : getEmptyBgColor());
        floating_btn.setVisibility(setFloatingButtonVisibility() ? View.VISIBLE : View.GONE);
        if (setFloatingButtonImage() != 0) {
            floating_btn.setBackground(this.getResources().getDrawable(setFloatingButtonImage()));
        }

        adapter = getAdapter();
        if (adapter != null) {
            recycleview.setAdapter(adapter);
        }
        if (getHeaderLayout(header_layout) != null) {
            header_layout.addView(getHeaderLayout(header_layout));
        }
        if (getFooterLayout(bottom_layout) != null) {
            setMargen(40);
            bottom_layout.addView(getFooterLayout(bottom_layout));
        } else {
            setMargen(60);
        }
        loadinglayout.setRetryListener(this);
        refreshLayout.setEnableLoadMore(getLoadMoreEnabled());
        refreshLayout.setEnableRefresh(getRefreshEnabled());
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh(false);
            }
        });

        floating_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnFloatingButtonClickListener();
            }
        });
        refresh(true);
    }

    protected void setTitleBar(TitleBar title_bar) {

    }

    /**
     * 用来再请求前接收和初始化
     */
    protected void receiveIntentExtras() {
    }


    public int getTitleBarColor() {
        return 0;
    }

    /**
     * 设置空布局北京颜色
     *
     * @return
     */
    public int getEmptyBgColor() {
        return 0;
    }

    public String getTitleText() {
        return "";
    }


    public boolean getLoadMoreEnabled() {
        return true;
    }

    public boolean getStandardSmartLoad() {
        return true;
    }

    public boolean getRefreshEnabled() {
        return true;
    }

    /**
     * 设置悬浮按钮 显示隐藏
     *
     * @return
     */
    public boolean setFloatingButtonVisibility() {
        return isFloatingButtonVisibility;
    }

    /**
     * 动态设置设置悬浮按钮 显示隐藏
     *
     * @return
     */
    public void setFloatingButtonVisibility(boolean isShow) {
        floating_btn.setVisibility(isShow ? View.VISIBLE : View.GONE);
        isFloatingButtonVisibility = isShow;
    }

    /**
     * 设置悬浮按钮背景图片
     *
     * @return
     */
    public int setFloatingButtonImage() {
        return 0;
    }

    /**
     * 设置悬浮按钮点击事件
     */
    public void setOnFloatingButtonClickListener() {

    }

    /**
     * @author yinsh
     * @description 传递adapter
     * @date 2021/8/19 上午11:01
     */
    public BaseQuickAdapter getAdapter() {
        return null;
    }

    public View getHeaderLayout(RelativeLayout parent) {
        return null;
    }

    public View getFooterLayout(RelativeLayout parent) {
        return null;
    }

    @Override
    public void onErrorclick() {
        refresh(true);
    }

    public void refresh(boolean isloading) {
        isrefresh = true;
        getDataRequest(0, isloading);
    }

    private void loadMore() {
        isrefresh = false;
        getDataRequest(mData.size(), false);
    }

    /**
     * @author yinsh
     * @description 回到顶部
     * @date 2021/12/21 上午9:56
     */
    private void scrollToTop() {
        try {
            if (recycleview != null && refreshLayout != null) {
                recycleview.smoothScrollToPosition(0);
                recycleview.scrollToPosition(0);
                refreshLayout.closeHeaderOrFooter();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getDataRequest(int listSize, boolean isloading) {

    }

    /**
     * @author yinsh
     * @description 请求成功
     * @date 2021/8/19 上午10:53
     */
    public void loadSuccess(boolean hasNext, List<T> datas) {
        if (isFinishing()) {
            return;
        }
        if (isrefresh) {
            mData.clear();
            scrollToTop();
        }
        if (datas != null) {
            mData.addAll(datas);
        }
        adapter.setNewData(mData);
//        adapter.setList(mData);
        adapter.notifyDataSetChanged();

        if (getStandardSmartLoad()) {
            ll_content.setVisibility(View.VISIBLE);
            not_login_layout.setVisibility(View.GONE);
            if (getLoadMoreEnabled()) {
                floating_btn.setVisibility(setFloatingButtonVisibility() ? View.VISIBLE : View.GONE);
                SmartLoadUtil.canLoadMore(mData, hasNext, refreshLayout, loadinglayout, isrefresh,isShowContent());
            } else {
                if (setFloatingButtonVisibility()) {
                    floating_btn.setVisibility(View.GONE);
                }
                SmartLoadUtil.forbidLoadMore(mData, refreshLayout, loadinglayout, isrefresh,isShowContent());
            }
        } else {
            if (mData.isEmpty()) {
                not_login_layout.setVisibility(View.VISIBLE);
                ll_content.setVisibility(View.GONE);
                onChangeCustomPage();
            } else {
                ll_content.setVisibility(View.VISIBLE);
                not_login_layout.setVisibility(View.GONE);
                if (getLoadMoreEnabled()) {
                    floating_btn.setVisibility(setFloatingButtonVisibility() ? View.VISIBLE : View.GONE);
                    SmartLoadUtil.canLoadMore(mData, hasNext, refreshLayout, loadinglayout, isrefresh,isShowContent());
                } else {
                    if (setFloatingButtonVisibility()) {
                        floating_btn.setVisibility(View.GONE);
                    }
                    SmartLoadUtil.forbidLoadMore(mData, refreshLayout, loadinglayout, isrefresh,isShowContent());
                }
            }

        }
    }

    public boolean isShowContent() {
        return isshowContent;
    }


    public void onChangeCustomPage() {

    }

    /**
     * @author yinsh
     * @description 请求成功
     * @date 2021/8/19 上午10:52
     */
    public void loadFalied() {
        if (isFinishing()) {
            return;
        }
        if (setFloatingButtonVisibility()) {
            floating_btn.setVisibility(View.GONE);
        }
        if (getLoadMoreEnabled()) {
            SmartLoadUtil.canFailLoadMore(refreshLayout, loadinglayout, isrefresh,isShowContent());
        } else {
            SmartLoadUtil.forbidFailLoadMore(mData, refreshLayout, loadinglayout, isrefresh,isShowContent());
        }
    }

    /**
     * 创建Presenter
     *
     * @return
     */
    protected abstract P createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //presenter断开与界面的联系
        try {
            if (mPresenter != null) {
                mPresenter.detachView();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setMargen(int dp) {
        RelativeLayout.LayoutParams layoutParams =
                new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, dp2px(20), dp2px(dp));//4个参数按顺序分别是左上右下
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        floating_btn.setLayoutParams(layoutParams);
    }

    private int dp2px(float dipValue) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


}
